// JavaScript Document
/*
首页大图轮换函数，需要jquery。
*/
if (!Array.prototype.shuffle) {
    Array.prototype.shuffle = function() {
        for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
        return this;
    };
}
Array.prototype.PicChanger=function(targetID){
	var This = this;
	if(arguments.length>1)
	var pc=new WP_PicChanger(This,targetID,arguments[1]);
	else
	var pc=new WP_PicChanger(This,targetID);
	pc.init();
}
Array.prototype.Random=function(targetID){
	var This = this;
	var pc=new WP_RandomList(This,targetID);
	pc.init();
}
Array.prototype.PicChoice=function(targetIDS){
	var This = this;
	var pc=new WP_PicChoice(This,targetIDS);
	pc.init();
}
WP_RandomList = function(data,targetID){
	this.targetID = targetID;
	this.data = data;
}
WP_PicChoice = function(data,targetIDS){
	this.targetIDS = targetIDS;
	this.data = data;
}
WP_PicChoice.prototype = {
	init:function(){
		var dhtml = '';
		var This = this;
		for(var i=0;i<this.data.length;i++){
			dhtml += '\
    	<li data-index='+i+'>\
      	<a href="javascript:void(0);" title="'+this.data[i].title+'">\
        	<img src="'+this.data[i].smallimg+'" />\
          <span>\
          	'+this.data[i].title+'<br />\
            <em>了解更多</em>\
          </span>\
        </a>\
      </li>';
		}
		$("#"+this.targetIDS[0]+"").html(dhtml);
		
    $("#"+this.targetIDS[0]+" li").live("mouseover",function(){
			$("#"+This.targetIDS[0]+" li.hover").removeClass("hover");
			$(this).addClass("hover");
		});
    $("#"+this.targetIDS[0]+" li").live("mouseout",function(){
			$("#"+This.targetIDS[0]+" li.hover").removeClass("hover");
		});
    $("#"+this.targetIDS[0]+" li").live("click",function() {
			$(this).animate({left:'450px',opacity:'0.2'/*,rotate:'25deg'*/},500,function(){
				$(this).animate({/*rotate:'0deg'*/},500,function(){
					$(this).hide('slow',function(){$(this).addClass('actived')});
					$('#'+This.targetIDS[0]+' li.actived').animate({left:'',opacity:1},500);
					$('#'+This.targetIDS[0]+' li.actived').show('slow',function(){$(this).removeClass('actived');});
				});
						
				var data = This.data[$(this).attr("data-index")];
				$("#"+This.targetIDS[1]+" li").animate({opacity:0.1},500,function(){
					$("#"+This.targetIDS[1]+" img").attr("src",data.bigimg);
					$("#"+This.targetIDS[1]+" span").html(data.title);
					$("#"+This.targetIDS[1]+" p").html(data.description+'<a href="'+data.link+'">了解更多</a>');
					$("#"+This.targetIDS[1]+" li").animate({opacity:1},500);
				});
			});
		});
		$("#"+this.targetIDS[2]+" a.PreItem").live("click",function(){
			var currentIndex = parseInt($('#'+This.targetIDS[0]+' li.actived:eq(0)').attr("data-index"),10);
			if(currentIndex==0){
				currentIndex = This.data.length;
			}
			$("#"+This.targetIDS[0]+" li:eq("+(currentIndex-1)+")").trigger("click");
		});
		$("#"+this.targetIDS[2]+" a.NextItem").live("click",function(){
			var currentIndex = parseInt($('#'+This.targetIDS[0]+' li.actived:eq(0)').attr("data-index"),10);
			if(currentIndex==This.data.length-1){
				currentIndex = -1;
			}
			$("#"+This.targetIDS[0]+" li:eq("+(currentIndex+1)+")").trigger("click");
		});
		$("#"+this.targetIDS[0]+" li:eq(0)").trigger("click");
	}
}
WP_RandomList.prototype = {
	targetID:null,
	data:null,
	begin:0,
	cls:['Modern1-1','Modern2-2','Modern2-1','Modern1-2','Modern2-1','Modern2-2','Modern2-1','Modern2-2'],
	html:'',
	init:function(){
		this.data.shuffle();
		for(var i=0;i<this.data.length;i++){
			this.html += '<li class="'+this.cls[i % this.cls.length]+'"><a href="'+this.data[i].link+'">'+this.data[i].title+'</a></li>';
		}
		document.getElementById(this.targetID).innerHTML = this.html;
	}
}
WP_PicChanger = function(data,targetID){ 
	this.targetID=targetID;
	this.data=data;
	if(arguments.length>2) this.delay=arguments[2];
}
WP_PicBox = function(data,targetID){ 
	this.targetID=targetID;
	this.data=data;
}
WP_PicChanger.prototype = {
	targetID:null,
	data:null,
	index:0,
	delay:3500,
	speed:500,
	timer:0,
	opacity:0.05,
	newslink:null,
	piclink:null,
	newspic:null,
	newscontent:null,
	gonext:function(){
		if(this.data.length == this.index+1) this.index=-1;
		this.goto(this.index+1);
	},
	goprevious:function(){
		if(this.index == 0) this.index = this.data.length;
		this.goto(this.index-1);
	},
	pause:function(){
		window.clearInterval(this.timer);
	},
	start:function(){
		var This = this;
		this.timer = window.setInterval(function(){This.gonext();},This.delay);
	},
	goto:function(index){
		var This = this;
		if(arguments[1]){
				This.newslink.attr("href",This.data[index].link);
				This.newslink.html(This.data[index].title);
				This.newslink.attr("title",This.data[index].title);
				This.piclink.attr("href",This.data[index].link);
				This.piclink.attr("title",This.data[index].title);
				This.newspic.attr("src",This.data[index].image);
				This.newscontent.html(This.data[index].description);
				This.index=index;
				
				$("#"+This.targetID+" li.ControlArea ul li a.hover").removeClass("hover");
				$("#"+This.targetID+" li.ControlArea ul li a:eq("+This.index+")").addClass("hover");
		}else{
			$("#"+This.targetID+" li.PicContent").fadeTo(this.speed,this.opacity,function(){
						This.newslink.attr("href",This.data[index].link);
						This.newslink.html(This.data[index].title);
						This.newslink.attr("title",This.data[index].title);
						This.piclink.attr("href",This.data[index].link);
						This.piclink.attr("title",This.data[index].title);
						This.newspic.attr("src",This.data[index].image);
						This.newscontent.html(This.data[index].description);
						This.index=index;
						$("#"+This.targetID+" li.PicContent").fadeTo(This.speed,1);
						
						$("#"+This.targetID+" li.ControlArea ul li a.hover").removeClass("hover");
						$("#"+This.targetID+" li.ControlArea ul li a:eq("+This.index+")").addClass("hover");
					}
			);
		}
	},
	/*
	初始化事件。
	*/
	init:function(){
		var This = this;
		var areahtml = '';
		for(var i=0;i<this.data.length;i++){
			areahtml+='<li><a href="javascript:;"></a></li>\r\n';
			//图片预加载
			var imgpre=new Image();
			imgpre.src=this.data[i].image;
		}
		$("#"+This.targetID+" li.ControlArea ul").html(areahtml);
		$("#"+This.targetID+" li.ControlBtn ul li.right").click(function(e) {
      This.gonext();
    });
		$("#"+This.targetID+" li.ControlBtn ul li.left").click(function(e) {
      This.goprevious();
    });
		$("#"+This.targetID+" li.ControlArea ul li a").each(function(index, element) {
			var Super = This;
		  $(this).click(function(e) {
			Super.goto(index);
		  });
    });
		
		this.newslink=$("#"+This.targetID+" li.PicContent ul li a:empty");
		this.piclink=$("#"+This.targetID+" li.PicContent ul li a:not(:empty)");
		this.newspic=$("#"+This.targetID+" li.PicContent ul li img");
		this.newscontent=$("#"+This.targetID+" li.PicContent ul li p");
		
		$("#"+This.targetID).hover(function(){This.pause();},function(){This.start();});
		this.goto(0,true);
		this.start();		
	}
}

//TAB切换代码
function tab(h){
	var hs = h.getAttribute("t").split(" ");
	for(var q=0;q<hs.length;q++){
		var c=arguments.length>q?arguments[q+1]:arguments[arguments.length-1];
		var tid=document.getElementById("tab_"+hs[q]);
		var p=h.parentNode;
		var ts=p.childNodes;
		for(var i=0;i<ts.length;i++){
				if(ts[i].nodeType==3 && !/\S/.test(ts[i].nodeValue)){
				//Ignore This whiteSpaceNode
			}else{
				ts[i].className=ts[i].className.replace(/\s?hover$/,"");
			}
		}
		h.className+=" hover";
		ts=tid.parentNode.childNodes;
		for(var i=0;i<ts.length;i++){
			if(ts[i].nodeType==3 && !/\S/.test(ts[i].nodeValue)){
				//Ignore This whiteSpaceNode
			}else{
				ts[i].className= ts[i].className + " hidden";
			}
		}
		if(h.getAttribute("l")&&h.getElementsByTagName("a")){
			document.getElementById("tab_"+h.getAttribute("l")).href=h.getElementsByTagName("a")[0].href;
		}
		//tid.className=c||"TabList_Pic";
		//alert(tid.className.replace(/\s*hidden/gi,''));
		tid.className=tid.className.replace(/\s*hidden/gi,'');
	}
}
//DropSelect
WP_DropSelect=function(){}
WP_DropSelect.prototype={
	objs:null,
	removed:0,
	timer:0,
	cls:'',
	callback:'',
	init:function(){
		this.objs=new Array();
		var selects = document.getElementsByTagName("select");
		for(var i=0;i<selects.length;i++){
			if(selects[i].getAttribute("rel")=="dropselect"){
				this.replace(selects[i]);
			}
		}
		while(selectitem=this.hasdropselect()){
			selectitem.parentNode.removeChild(selectitem);
		}
	},
	hasdropselect:function(){
		var selects = document.getElementsByTagName("select");
		for(var i=0;i<selects.length;i++){
			if(selects[i].getAttribute("rel")=="dropselect"){
				return selects[i];
			}
		}
		return false;
	},
	replace:function(obj){
		var cls=obj.getAttribute("cls");
		var name=obj.getAttribute("name");
		var callback=obj.getAttribute("call");
		var zindex=obj.getAttribute("zindex");
		
		var dtag=document.createElement("span");
		if(obj.getAttribute("width")){
			dtag.style.width=obj.getAttribute("width");
		}
		if(obj.getAttribute("height")){
			dtag.style.height=obj.getAttribute("height");
		}
		dtag.className='dropselect_box dropselect_box_'+cls;
		var dtagul=document.createElement("ul");
		dtagul.setAttribute("dropselect_name",name);
		dtagul.className="dropselect_"+cls;
		if(obj.getAttribute("zindex")){
			dtag.style.zIndex=obj.getAttribute("zindex");
		}
		var This = this;
		var opts=this.getoptions(obj);
		var sObj = obj;
		for(var i=0;i<opts.length;i++){
			var option=document.createElement("li");
			option.setAttribute("dropselect_value",opts[i].value);
			//option.innerHTML='<a href="javascript:;" title="'+opts[i].text+'">'+opts[i].text+'</a>';
			var linkitem = document.createElement("a");
			linkitem.setAttribute("href","javascript:;");
			linkitem.setAttribute("title",opts[i].text);
			linkitem.innerHTML=opts[i].text;
			if(i!=obj.selectedIndex){option.className='hidden';option.setAttribute("selected","false");}else{option.className='hover';option.setAttribute("selected","true");}
			linkitem.onclick=function(){
				var fieldname=this.parentNode.parentNode.getAttribute("dropselect_name");
				var fieldvalue=this.parentNode.getAttribute("dropselect_value");
				var lis=this.parentNode.parentNode.getElementsByTagName("li");

				if(this.parentNode.parentNode.getAttribute("expanded")=="false"){
					for(var i=0;i<lis.length;i++){
						if(lis[i].className!="hover") lis[i].className='';
					}
					this.parentNode.parentNode.setAttribute("expanded","true");
					this.parentNode.parentNode.className += ' dropselect_'+cls+'_expanded';
					
					
					if(sObj.getAttribute("dropheight")){
						var dropheight = sObj.getAttribute("dropheight").replace('px','');
						if(this.parentNode.parentNode.scrollHeight > dropheight) this.parentNode.parentNode.style.height=dropheight+'px';
						this.parentNode.parentNode.style.overflow='auto';
					}
				}else{
					for(var i=0;i<lis.length;i++){
						lis[i].setAttribute("selected","false");
					}
					this.parentNode.setAttribute("selected","true");
					
					var fields=document.getElementsByName(fieldname);
					for(var i=0;i<fields.length;i++){
						fields[i].value=fieldvalue;
						fields[i].setAttribute("value",fieldvalue);
					}
					
					for(var i=0;i<lis.length;i++){
						if(lis[i].getAttribute("selected")=="false") lis[i].className='hidden'
					}
					
					this.parentNode.className='hover';
					
					this.parentNode.parentNode.setAttribute("expanded","false");
					this.parentNode.parentNode.className=this.parentNode.parentNode.className.replace(' dropselect_'+cls+'_expanded','');
					
					this.parentNode.parentNode.style.height = 'auto';
					this.parentNode.parentNode.style.overflow='hidden';
					
					if(callback && typeof window[callback] == "function"){
						window[callback](this.parentNode.getAttribute("dropselect_value"),this.parentNode.parentNode.getAttribute("dropselect_name"));
					}
				}
			}
			option.appendChild(linkitem);
			dtagul.appendChild(option);
		}
		dtagul.setAttribute("expanded","false");
		/*if(obj.getAttribute("dropheight")){
			var dropheight = obj.getAttribute("dropheight").replace('px','');
			dtagul.setAttribute('style','max-height:'+dropheight+'px;_height:expression(this.scrollHeight > '+dropheight+' ? "'+dropheight+'px":"");overflow:auto;');
		}*/
		var hiddenfield = document.createElement("input");
		//想设置name属性，ie6 ie7中必须这么写 document.createElement("<input name='select'>")
		//判断浏览器版本 ff/ie
		var userAgent = navigator.userAgent; 
		var isOpera = userAgent.indexOf("Opera") > -1;
		if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera)
		{
			try{hiddenfield=document.createElement("<input name='"+name+"'>");}catch(o){}
		} ; //判断是否IE浏览器
		hiddenfield.name=name;
		hiddenfield.setAttribute("name",name);
		hiddenfield.setAttribute("type","hidden");
		if(obj.getAttribute("id")){
			hiddenfield.id=obj.getAttribute("id");
			dtag.id="drop_"+obj.getAttribute("id");
		}
		hiddenfield.value=opts[obj.selectedIndex].value;
		hiddenfield.setAttribute("value",opts[obj.selectedIndex].value);
		
		dtag.appendChild(dtagul);
		var Obj = this.objs;
		var This = this;
		dtagul.onmouseout=function(){
			var sObj = Obj;
			var Super = This;
			This.timer = window.setTimeout(function(){
				for(var i=0;i<sObj.length;i++){
					if(sObj[i].getAttribute("expanded")=="true"){
						var lis=sObj[i].getElementsByTagName("li");
						for(var j=0;j<lis.length;j++){
							if(lis[j].getAttribute("selected")=="false") lis[j].className='hidden'
						}
						sObj[i].setAttribute("expanded","false");
						sObj[i].className=sObj[i].className.replace(' dropselect_'+cls+'_expanded','');
						
						sObj[i].style.height = 'auto';
						sObj[i].style.overflow='hidden';
					}
				}
			},10);
		}
		dtagul.onmouseover=function(){
			window.clearTimeout(This.timer);
		}
		
		this.objs.push(dtagul);
		obj.parentNode.insertBefore(hiddenfield,obj);
		obj.parentNode.insertBefore(dtag,obj);
	},
	getoptions:function(obj){
		var retval=new Array();
		var option=obj.options;
		for(var i=0;i<option.length;i++){
			retval.push({text:option[i].innerHTML,value:option[i].value});
		}
		return retval;
	}
}
function $DivNav(){
	this.target=document.body;
	this.current=null;
	this.Version="1.0.0";
	this.cElement=null;
	this.timer=0;
	this.defaultshow = null;
};
var DivNav=new $DivNav();
DivNav.init=function(){
		var This=this;
		if(arguments.length>0){
			if(document.getElementById(arguments[0])){
				this.target=document.getElementById(arguments[0]);
			}
		}
		if(arguments.length>1){
			if(document.getElementById(arguments[1])){
				this.defaultshow=document.getElementById(arguments[1]);
			}
		}
		
		this.navList=this.target.getElementsByTagName("a");
		for(var i=0;i<this.navList.length;i++){
			this.navList[i].onmouseover=function(){
				This.show(This);
			}
			this.navList[i].onmouseout=function(){
				This.hidden(This);
			}
		}
	}
DivNav.show=function(t){
		try{DivNav.cElement.parentNode.className=DivNav.cElement.parentNode.className.replace(/\shover/gi,"");}catch(o){}
		var e = window.event || this.show.caller.arguments[0];
    	DivNav.cElement = e.srcElement || e.target;
		if(DivNav.cElement.tagName != "A"){
			DivNav.cElement = DivNav.cElement.parentNode;
		}
		DivNav.cElement.parentNode.className+=" hover";
		DivNav.target=document.getElementById(DivNav.cElement.getAttribute("forDiv"));
		//alert(DivNav.cElement.getAttribute("forDiv"));
		DivNav.target.onmouseover=function(){
			clearTimeout(DivNav.timer);
		}
		DivNav.target.onmouseout=function(){
			DivNav.timer=setTimeout(DivNav.dohidden,50);
		}
		if( typeof(DivNav.target)=="undefined" || DivNav.target==null){
			return;
		}
		try{DivNav.current.className+=" hidden";}catch(o){}
		clearTimeout(DivNav.timer);
		DivNav.target.className=DivNav.target.className.replace(/\s*hidden/gi,"");
		//this.timer=setTimeout(this.dohidden,300);
		this.defaultshow.className+=" hidden";
		DivNav.current=DivNav.target;
		
	}
DivNav.hidden=function(){
		DivNav.timer=setTimeout(DivNav.dohidden,50);
	}
DivNav.dohidden=function(){
		clearTimeout(DivNav.timer);
		try{DivNav.current.className+=" hidden";}catch(o){}
		DivNav.cElement.parentNode.className=DivNav.cElement.parentNode.className.replace(/\shover/gi,"");
		DivNav.defaultshow.className = DivNav.defaultshow.className.replace(/\s*hidden/gi,"");
	}
//加入收藏
function AddFavorite(sURL, sTitle) {
		sURL = encodeURI(sURL);
		try{
				window.external.addFavorite(sURL, sTitle);
		}catch(e) {
				try{
						window.sidebar.addPanel(sTitle, sURL, ""); 
				}catch (e) { 
						alert("加入收藏失败，请使用Ctrl+D进行添加,或手动在浏览器里进行设置.");
				}  
		}
}
 
//设为首页
function SetHome(url){
		if (document.all) {
				document.body.style.behavior='url(#default#homepage)';
					 document.body.setHomePage(url);
		}else{
				alert("您好,您的浏览器不支持自动设置页面为首页功能,请您手动在浏览器里设置该页面为首页!");
		}
}


function getData(url,targetId,sourceId){
	$.ajaxSettings.async = true;
	$.ajax({url:url,
			type:"get",        
			dataType:'html',			 
			data:"",
			success:function(msg){
				msg="<div>"+msg+"</div>";
				if(sourceId!=null && sourceId!=""){
					$("#"+targetId).html($(msg).find("#"+sourceId).html());
				}else{
					$("#"+targetId).html($(msg).html());
				}
			}});
}


$(document).ready(function(e) {
	var dropselect = new WP_DropSelect();
	dropselect.init();
	var added = false;
	/**
	var navEvent = function(e) {
		if(($('body').scrollTop()>0?$('body').scrollTop():$('html').scrollTop())>110){
			$(".topnav").addClass("follownav").removeClass("topnav");
			if(!added){
				$(".main").css("padding-top",parseInt($(".main").css("padding-top"))+110+"px");
				$(".header").css("padding-top",parseInt($(".header").css("padding-top"))+110+"px");
				added = true;
			}
		}else{
			$(".follownav").addClass("topnav").removeClass("follownav");
			if(added){
				$(".main").css("padding-top",parseInt($(".main").css("padding-top"))-110+"px");
				$(".header").css("padding-top",parseInt($(".header").css("padding-top"))-110+"px");
				added = false;
			}
		}
	}
	$(window).scroll(navEvent);
	navEvent();**/
});
function getLink(value,name){
	//alert(value);
	if(value!="")top.location.href=value;
}